package com.examiner.controller;

import com.examiner.bean.Job;
import com.examiner.bean.vo.JobLowerVo;
import com.examiner.result.Result;
import com.examiner.service.JobService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/job")
@RestController
public class JobController {
    @Resource
    private JobService jobService;

    /**
     * 根据id查询当前职位
     * @param id
     * @return
     */
    @PostMapping("/getJobById/{id}")
    public Result<Object> getJobById(@PathVariable("id")Integer id){
        Job job;
        try{
            job = jobService.getJobById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"获取职位信息失败",false,null);
        }
        if (job == null){
            return Result.RESULT(201,"当前职位不存在",false,null);
        }
        return Result.RESULT(200,"成功获取职位信息",true,job);
    }

    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    @PostMapping("/deleteJobById/{id}")
    public Result<Object> deleteJobById(@PathVariable("id")Integer id){
        try{
            jobService.deleteJobById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"删除职位信息失败",false,null);
        }
        return Result.RESULT(200,"成功删除职位信息",true,null);
    }

    /**
     * 修改职位信息
     * @param job
     * @return
     */
    @PostMapping("/changeJob")
    public Result<Object> changeJob(@RequestBody Job job){
        try{
            jobService.changeJobInfo(job);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"修改职位信息失败",false,null);
        }
        return Result.RESULT(200,"成功修改职位信息",true,null);
    }

    /**
     * 新增职位
     * @param job
     * @return
     */
    @PostMapping("/addJob")
    public Result<Object> addJob(@RequestBody Job job){
        try{
            Job exist = jobService.getJobByUserName(job.getUserName());
            if (exist != null){
                return Result.RESULT(201,"该职位已存在",false,null);
            }
            jobService.addJob(job);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"添加职位信息失败",false,null);
        }
        return Result.RESULT(200,"成功添加职位信息",true,null);
    }

    /**
     * 获取当前职位的直接下级
     * @param id
     * @return
     */
    // TODO
    @PostMapping("/getJobLower/{id}")
    public Result<Object> getJobLower(@PathVariable("id")Integer id){
        JobLowerVo jobLowerVo;
        try{
            jobLowerVo = jobService.getLower(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"查询职位下级信息失败",false,null);
        }
        return Result.RESULT(200,"成功查询职位下级信息",true,jobLowerVo);
    }


    /**
     * 获取当前职位的所有下级
     * @param id
     * @return
     */
    // TODO
    @PostMapping("/getJobAllLower/{id}")
    public Result<Object> getJobAllLower(@PathVariable("id")Integer id){
        JobLowerVo jobLowerVo;
        try{
            jobLowerVo = jobService.getAllLower(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"查询职位下级信息失败",false,null);
        }
        return Result.RESULT(200,"成功查询职位下级信息",true,jobLowerVo);
    }



}
